/**
 * Javascript file for sources needed at allyinformation.php
 */

var store1 = null;

function galaxytool_allystatus(id, NewStatus) {
	var alliance_id  = parseInt(id);
	if (alliance_id < 1) return;
	
	var xhrArgs = {
			url: "ajax/ajax_stats.php?type=change_alliance&alliance_id="+alliance_id+"&new_status="+NewStatus,
			handleAs: "text",
			load: function(text) {
				if (text == "true") {
					// change color
					dojo.byId("pn_"+alliance_id).setAttribute("class", NewStatus);
				}
			},
			error: function(error) {
				console.log("Error:");
				console.log(error);
			}
	}	
	dojo.xhrPost(xhrArgs);
}

function galaxytool_centered_formatter(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	return val;
}

function galaxytool_score_formatter(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	return NumberFormat(val);
}

function galaxytool_player_formatter(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	cell.customClasses.push("hyperlink");
	
	var dipl_status = store1.getValues(grid1.getItem(rowIdx),"dipl_status");
	
	if (dipl_status != "nothing") {
		val = "<span class=\""+dipl_status+"\">"+val+"</span>";
	}	
	return val;
}

function galaxytool_member_cellclick(e) {
	if (e.cellIndex != 0) return;

	var id = e.grid.store.getValue(e.grid.getItem(e.rowIndex),"player_id");
	var url = "playerinformation.php?id="+id;
	window.location.href = url;
}

function galaxytool_allyinformation_init() {
	
	dojo.connect(dijit.byId("reporttooltip"), "onMouseLeave", galaxytool.report_display.CloseReportTooltip );
	dojo.connect(dijit.byId("reporttooltip"), "onMouseOver", galaxytool.report_display.ReportTooltipMouseOver );
	
	// select user defined tab
	var choosen_tab = window.location.hash;
	if (choosen_tab != "") {
		try {
			choosen_tab = choosen_tab.replace(/#select_/, "");
			dijit.byId("tabcontainer").selectChild(dijit.byId(choosen_tab));
		} catch (ignore) {
			// select default tab in case of wrong user input
		}
	}

	dojo.subscribe("tabcontainer-selectChild", function(child) {
		window.location.hash = "#select_" + child.get("id");
		// disable message area
		dojo.byId("messageArea").style.display  = "none";
	});
	
	// define store with data
	store1 = new dojo.data.ItemFileReadStore({data: alliance_member});
	store1.comparatorMap={};
	
	store1.comparatorMap["playername"] = function (a, b) { return a.localeCompare(b); };
	
	store1.comparatorMap["score"] = galaxytool_compare_integer;
	store1.comparatorMap["economy"] = galaxytool_compare_integer;
	store1.comparatorMap["fleet"] = galaxytool_compare_integer;
	store1.comparatorMap["research"] = galaxytool_compare_integer;
	grid1.setStore(store1);
	
	dojo.connect(grid1, "onCellClick", galaxytool_member_cellclick);
}

dojo.ready(galaxytool_allyinformation_init);
